package com.esri.core.map;

import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.MapGeometry;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.util.d;
import com.esri.core.symbol.Symbol;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;

/* loaded from: classes.dex */
public class Graphic implements Feature, Serializable {
    private static final String INTERNAL_GRAPHIC_UNIQUE_ID = "internalGraphicUniqueID";
    private static final long serialVersionUID = 1;
    protected final Map<String, Object> attributes;
    private int drawOrder;
    private Geometry geometry;
    SpatialReference spatialReference;
    private Symbol symbol;
    private int uid;

    public int getUid() {
        return this.uid;
    }

    public Graphic(Geometry geometry, Symbol symbol) {
        this.symbol = null;
        this.spatialReference = null;
        this.uid = -1;
        this.drawOrder = 0;
        this.attributes = Collections.synchronizedMap(new LinkedHashMap());
        this.geometry = geometry;
        this.symbol = symbol;
    }

    public Graphic(Geometry geometry, Symbol symbol, int i) {
        this(geometry, symbol);
        this.drawOrder = i;
    }

    public Graphic(Geometry geometry, Symbol symbol, Map<String, Object> map) {
        this.symbol = null;
        this.spatialReference = null;
        this.uid = -1;
        this.drawOrder = 0;
        Map<String, Object> synchronizedMap = Collections.synchronizedMap(new LinkedHashMap());
        this.attributes = synchronizedMap;
        this.geometry = geometry;
        this.symbol = symbol;
        setAttributes(map);
        if (synchronizedMap == null || !synchronizedMap.containsKey(INTERNAL_GRAPHIC_UNIQUE_ID)) {
            return;
        }
        Integer num = (Integer) synchronizedMap.get(INTERNAL_GRAPHIC_UNIQUE_ID);
        this.uid = num != null ? num.intValue() : -1;
        synchronizedMap.remove(INTERNAL_GRAPHIC_UNIQUE_ID);
    }

    public Graphic(Geometry geometry, Symbol symbol, Map<String, Object> map, int i) {
        this(geometry, symbol, map);
        this.drawOrder = i;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Graphic(int i, Geometry geometry, Symbol symbol, Map<String, Object> map) {
        this(geometry, symbol, map);
        this.uid = i;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Graphic(int i, Geometry geometry, Symbol symbol, Map<String, Object> map, int i2) {
        this(i, geometry, symbol, map);
        this.drawOrder = i2;
    }

    Graphic() {
        this.symbol = null;
        this.spatialReference = null;
        this.uid = -1;
        this.drawOrder = 0;
        this.attributes = Collections.synchronizedMap(new LinkedHashMap());
    }

    @Override // com.esri.core.map.Feature
    public Symbol getSymbol() {
        Symbol symbol = this.symbol;
        if (symbol != null) {
            try {
                return symbol.copy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void setSymbol(Symbol symbol) {
        this.symbol = symbol;
    }

    @Override // com.esri.core.map.Feature
    public Geometry getGeometry() {
        Geometry geometry = this.geometry;
        if (geometry == null) {
            return null;
        }
        return geometry;
    }

    void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }

    void setAttributes(Map<String, Object> map) {
        if (map != null && map.size() > 0) {
            this.attributes.putAll(map);
        } else {
            this.attributes.clear();
        }
    }

    void addAttribute(String str, Object obj) {
        this.attributes.put(str, obj);
    }

    void removeAttribute(String str) {
        this.attributes.remove(str);
    }

    public String[] getAttributeNames() {
        Map<String, Object> map = this.attributes;
        if (map == null) {
            return null;
        }
        return (String[]) map.keySet().toArray(new String[0]);
    }

    @Override // com.esri.core.map.Feature
    public Object getAttributeValue(String str) {
        Map<String, Object> map = this.attributes;
        if (map == null) {
            return null;
        }
        return map.get(str);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Object setAttributeValue(String str, Object obj) {
        return this.attributes.put(str, obj);
    }

    void removeAllAttribute() {
        this.attributes.clear();
    }

    @Override // com.esri.core.map.Feature
    public Map<String, Object> getAttributes() {
        if (this.attributes == null) {
            return null;
        }
        return new LinkedHashMap(this.attributes);
    }

    @Override // com.esri.core.map.Feature
    public SpatialReference getSpatialReference() {
        return this.spatialReference;
    }

    public int getDrawOrder() {
        return this.drawOrder;
    }

    void setDrawOrder(int i) {
        this.drawOrder = i;
    }

    public static Graphic fromJson(JsonParser jsonParser) throws Exception {
        if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
            return null;
        }
        Graphic graphic = new Graphic();
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if ("geometry".equals(currentName)) {
                MapGeometry e = d.e(jsonParser);
                graphic.spatialReference = e.getSpatialReference();
                graphic.setGeometry(e.getGeometry());
            } else if ("attributes".equals(currentName)) {
                LinkedHashMap linkedHashMap = new LinkedHashMap();
                d.a(jsonParser, linkedHashMap);
                graphic.setAttributes(linkedHashMap);
            } else if ("symbol".equals(currentName)) {
                graphic.setSymbol(d.i(jsonParser));
            } else if ("drawOrder".equals(currentName)) {
                graphic.setDrawOrder(jsonParser.getIntValue());
            } else {
                jsonParser.skipChildren();
            }
        }
        return graphic;
    }

    public static String toJson(Graphic graphic) throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator createJsonGenerator = new MappingJsonFactory().createJsonGenerator(stringWriter);
        createJsonGenerator.writeStartObject();
        if (!graphic.getAttributes().isEmpty()) {
            Map<String, Object> attributes = graphic.getAttributes();
            createJsonGenerator.writeFieldName("attributes");
            createJsonGenerator.writeObject(attributes);
        }
        if (graphic.getGeometry() != null && graphic.getSpatialReference() != null) {
            createJsonGenerator.writeFieldName("geometry");
            createJsonGenerator.writeRawValue(GeometryEngine.geometryToJson(graphic.getSpatialReference(), graphic.getGeometry()));
        }
        createJsonGenerator.writeEndObject();
        createJsonGenerator.close();
        return stringWriter.getBuffer().toString();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Graphic [ID=");
        sb.append(this.uid);
        sb.append(", Geometry=");
        Geometry geometry = this.geometry;
        sb.append(geometry != null ? geometry.getType() : null);
        sb.append(", Symbol=");
        sb.append(this.symbol);
        sb.append(", Draw Order=");
        sb.append(this.drawOrder);
        sb.append("]");
        return sb.toString();
    }

    @Override // com.esri.core.map.Feature
    public long getId() {
        return getUid();
    }
}
